package com.eyolas.passwordmanager.dao;

import java.sql.SQLException;

import com.eyolas.passwordmanager.idao.IPasswordDao;
import com.eyolas.passwordmanager.model.Password;
import com.eyolas.passwordmanager.utils.SimpleCrypto;
import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.support.ConnectionSource;

/**
 * Password dao
 * @author Eyolas
 * @date 24 mai 2012
 */
public class PasswordDao extends BaseDaoImpl<Password, Integer> implements IPasswordDao{

	public PasswordDao(ConnectionSource connectionSource) throws SQLException {
		super(connectionSource, Password.class);
	}

	@Override
	public Password getFirst() throws SQLException {
		return queryForFirst(queryBuilder().prepare());
	}
	
	@Override
	public boolean updatePassword(String newPassword) {
		try {
			Password pass = getFirst();
			pass.setPassword(SimpleCrypto.encrypt(newPassword, newPassword));
			update(pass);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

}
